#include "mypacket.h"
#include <QTest>
#include <QCoreApplication>
#include <QSignalSpy>

class TestMyPacket : public QObject
{
    Q_OBJECT

private slots:
    void testConstructor();
    void testStartStop();
    void testPacketCaptureSignal();
};

void TestMyPacket::testConstructor()
{
    MyPacket packet("lo"); // 使用回环接口 "lo"
    QVERIFY(!packet.isRunning);
    QCOMPARE(packet.context, QString("lo"));
}

void TestMyPacket::testStartStop()
{
    MyPacket packet("lo");
    packet.start();
    QVERIFY(packet.isRunning);

    packet.stop();
    packet.wait(); // 等待线程结束
    QVERIFY(!packet.isRunning);
}

void TestMyPacket::testPacketCaptureSignal()
{
    MyPacket packet("lo");
    QSignalSpy spy(&packet, &MyPacket::captured);

    // 模拟一个数据包
    uint8_t dummyPacket[60] = {0};
    struct pcap_pkthdr dummyHeader;
    dummyHeader.ts.tv_sec = 1234567890;
    dummyHeader.ts.tv_usec = 0;
    dummyHeader.len = 60;
    dummyHeader.caplen = 60;

    // 手动调用回调函数
    MyPacket::etProtocolCallBack(
        reinterpret_cast<u_char*>(&packet),
        &dummyHeader,
        dummyPacket
        );

    // 检查信号是否被触发
    QCOMPARE(spy.count(), 1);
}

QTEST_MAIN(TestMyPacket)
// 必须包含 MOC 生成的代码
